package cn.wolfcode.p2p.mgrsite.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.util.AssertUtil;
import cn.wolfcode.p2p.base.util.JSONResult;

/**
 * 后台登录相关
 */
@Controller
public class LoginController {

	@Autowired
	private ILoginInfoService loginInfoService;

	/**
	 * 后台登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/userLogin")
	@ResponseBody
	public JSONResult userLogin(String username, String password) {
		JSONResult result = new JSONResult();
		try {
			LoginInfo loginInfo = loginInfoService.login(username, password, LoginInfo.TYPE_MANAGER);
			AssertUtil.isNotNull(loginInfo, "账户密码有误.");
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 后台主页面
	 */
	@RequestMapping("main")
	public String main(){
		return "main";
	}

}
